# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\ntlm_auth\rc4.py


class ARC4(object):
    state = None
    i = 0
    j = 0

    def __init__(self, key):
        if isinstance(key, str):
            key = [ord(c) for c in key]
        else:
            key = [c for c in key]
        self.state = [n for n in range(256)]
        j = 0
        for i in range(256):
            j = (j + self.state[i] + key[i % len(key)]) % 256
            self.state[i], self.state[j] = self.state[j], self.state[i]

    def update(self, value):
        chars = []
        random_gen = self._random_generator()
        for char in value:
            if isinstance(value, str):
                byte = ord(char)
            else:
                byte = char
            updated_byte = byte ^ next(random_gen)
            chars.append(updated_byte)

        return bytes(bytearray(chars))

    def _random_generator(self):
        while True:
            self.i = (self.i + 1) % 256
            self.j = (self.j + self.state[self.i]) % 256
            self.state[self.i], self.state[self.j] = (
                self.state[self.j],
                self.state[self.i],
            )
            yield self.state[(self.state[self.i] + self.state[self.j]) % 256]
